import { Component, OnInit, ViewChild } from '@angular/core';
import { ActivatedRoute } from '@angular/router';
import { NzModalService } from 'ng-zorro-antd/modal';
import { RecordedService } from '../../services/recorded.service';

@Component({
  selector: 'app-partner-recored-detail',
  templateUrl: './detail.component.html',
  styleUrls: ['./detail.component.less']
})
export class PartnerRecordedDetailComponent implements OnInit {
  @ViewChild('auditModal', { static: false }) auditModal!: any;
  formData: any = {};
  timeLineData: any = [];
  msg = '';
  id = '';

  constructor(public service: RecordedService, private route: ActivatedRoute, private nzModalService: NzModalService) {
    this.id = route.snapshot.params.id;
    this.getRecordedDetail(this.id);
  }

  ngOnInit(): void {}

  getRecordedDetail(id: string) {
    this.service.request(this.service.$api_get_recorded_record_detail, { id }).subscribe(res => {
      if (res) {
        this.formData = res;
       
      }
    });
    this.service.request(this.service.$api_get_getPartnerInvoiceEntryDetail, { id }).subscribe(res => {
      if (res) {
        // 处理流程节点数据
        // 流程是否结束
        console.log(res);
        // submitTime creattime
        let isEnd = false;
          if (res.sts == '2') {
            isEnd = true;
            this.timeLineData.push({ time: res.modifyTime, value: `入账成功`, color: 'green' });
          } else if (res.sts == '3'){
            isEnd = true;
            this.timeLineData.push({ time: res.modifyTime, value: `入账失败`, color: 'red' });
          }
        // if (res.agreeTime && res.refundStatus !== '4') {
        //   this.timeLineData.push({ time: res.agreeTime, value: `银行处理中`, color: 'gray' });
        // }
        if (res.reAuditTime) {
          if (res.sts === '2') {
            this.timeLineData.push({
              time: res.reAuditTime,
              value: `审核通过<br/>操作人员：${res.reAuditName}`,
              color: 'gray'
            });
          }
        }
        if (res.firstAuditTime) {
          if (res.sts === '1') {
            this.timeLineData.push({
              time: res.firstAuditTime,
              value: `审核通过<br/>操作人员：${res.fristAuditName}`,
              color: 'gray'
            });
          }
        }
        if (res.sts) {
          this.timeLineData.push({
            time: res.submitTime,
            value: `提交入账申请<br/>入账${res.entryAmount}元至${res.fictitiousAccount}<br/>操作人员：${res.submitUserName}`,
            color: 'gray'
          });
        }
        if (this.timeLineData?.length > 0 && !isEnd) {
          this.timeLineData[0].color = 'green';
        }
      }
    });
  }

  auditAction(item?: any, type: string = '1') {
    this.msg = '';
    let params: Array<string> = [];
    params = [item.id];
    const modal = this.nzModalService.create({
      nzTitle: type === '1' ? '审核' : '复审',
      nzContent: this.auditModal,
      nzFooter: [
        {
          label: '拒绝',
          type: 'default',
          onClick: () => {
            if (!this.msg || this.msg.trim().length === 0) {
              this.service.msgSrv.warning('请填写拒绝原因 ');
              return;
            }
            this.audit(
              { ids: params, rejectReason: this.msg, sts: '3' },
              () => {
                modal.destroy(true);
              },
              '审核拒绝成功'
            );
          }
        },
        {
          label: '通过',
          type: 'primary',
          onClick: () => {
            console.log(this.formData?.sts);

            this.audit(
              { ids: params, rejectReason: this.msg, sts: this.formData?.sts === '0' ? 1 : 2 },
              () => {
                modal.destroy(true);
              },
              `${this.formData?.sts === '1' ? '审核' : '复审'}通过成功`
            );
          }
        }
      ]
    });
    modal.afterClose.subscribe((res: any) => {
      if (res) {
        this.getRecordedDetail(this.id);
      }
    });
  }
  goBack() {
    history.go(-1);
  }

  /**
   * 审核
   */
  audit(params: any, callback: Function, msg = '成功') {
    this.service.request(this.service.$api_audit_recored, {...params}).subscribe(res => {
      if (res) {
        this.service.msgSrv.success(msg);
        callback();
      }
    });
  }
}
